//
//  HomePageController.m
//  traveling
//
//  Created by xiaoR on 16/3/27.
//  Copyright © 2016年 family. All rights reserved.
//

#import "HomePageController.h"
#import "AFNetworking.h"
#import "UIImageView+AFNetworking.h"
#import "FirstViewModel.h"
#import "DetailViewController.h"
#define URL (@"http://chanyouji.com/api/wiki/destinations.json")

@interface HomePageController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSMutableArray *_arr;
    NSMutableArray *_scrollViewArr;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray * dataArr;

@end

@implementation HomePageController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _dataArr = [[NSMutableArray alloc]init];
    _arr = [[NSMutableArray alloc]init];
//    [self initData];
//    [self createView];
    [self getData];
    [self createView];
    [self createNavigationBar:9];
    self.back.text = @"首页";
}

-(void)getData
{
    
    __weak typeof(self)weakSelf = self;
    
    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:URL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSArray * arr = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary * dic in arr) {
            NSArray *itemArr = dic[@"destinations"];
            JSONModelArray * arr1 = [[JSONModelArray alloc]initWithArray:itemArr modelClass:[FirstViewModel class]];
            
            [weakSelf.dataArr addObject:arr1];
        }
        
        [weakSelf.tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%@",error);
    }];
    
    
}

-(void)createView
{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 48, ScreenWidth, ScreenHeight - 64) style:0];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
//    注册celll
    [_tableView registerNib:[UINib nibWithNibName:@"RecomendCell" bundle:nil] forCellReuseIdentifier:@"111"];
}
#pragma mark - 代理方法 －
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return _dataArr.count;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //NSLog(@"%ld",[[_dataArr objectAtIndex:section] count]);
    return [[_dataArr objectAtIndex:section] count];
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * arr = [_dataArr objectAtIndex:indexPath.section];
    FirstViewModel * model = [arr objectAtIndex:indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"111"];
    //填充数据
    UIImageView *imgV = (UIImageView *)[cell.contentView viewWithTag:1000];
    [imgV setImageWithURL:[NSURL URLWithString:model.image_url ] placeholderImage:[self createPlImage]];
    UILabel *lable = (UILabel *)[cell.contentView viewWithTag:1001];
    lable.text = model.name_zh_cn;
    
    UILabel *lable2 = (UILabel *)[cell.contentView viewWithTag:1002];
    lable2.text = model.name_en;
    
    return cell;
}
- (UIImage *)createPlImage{
    NSString *path = [[NSBundle mainBundle]pathForResource:@"pl" ofType:@"png"];
    return [UIImage imageWithContentsOfFile:path];
}

//返回每个cell的高度
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}

//透视图
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UILabel *lable = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 60)];
    lable.font = [UIFont systemFontOfSize:23];
    lable.textAlignment = 1;
    lable.textColor = [UIColor whiteColor];
    lable.backgroundColor = RGBA(44, 147, 53, .6);
    if (section == 0) {
        lable.text = @"亚洲国家";
    }else if(section == 1){
        lable.text = @"欧洲国家";
    }else if (section ==2){
        lable.text = @"新西兰";
    }else if (section == 3){
        lable.text = @"港澳台";
    }else{
        lable.text = @"国内城市";
    }
    return lable;
}



-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}
//为使图
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}


//点击了某个cell
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableArray * arr = [_dataArr objectAtIndex:indexPath.section];
    FirstViewModel * model = [arr objectAtIndex:indexPath.row];
    DetailViewController *detail = [[DetailViewController alloc]init];
    detail.arr = model.children;
    
    detail.hidesBottomBarWhenPushed = YES;
    detail.name = model.name_zh_cn;
    
    [self.navigationController pushViewController:detail animated:YES];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
